

import java.util.Iterator;

import assignmentimplementation.IOException_Exception;
import assignmentimplementation.KeyImpl;
import assignmentimplementation.KeyNotFoundException_Exception;
import assignmentimplementation.KeyValueBaseImpl;
import assignmentimplementation.KeyValueBaseImplService;
import assignmentimplementation.ServiceNotInitializedException_Exception;
import assignmentimplementation.ValueImpl;
import assignmentimplementation.ValueListImpl;




public class Main {

	/**
	 * @param args
	 * @throws ServiceNotInitializedException_Exception 
	 * @throws KeyNotFoundException_Exception 
	 * @throws IOException_Exception 
	 */
	public static void main(String[] args) throws IOException_Exception, KeyNotFoundException_Exception, ServiceNotInitializedException_Exception {

		long total = 0;
		int counter = 0;
		long startTime,endTime, initTime;
		KeyImpl k = new KeyImpl();
		ZipfGenerator distr = new ZipfGenerator(2393818, 0.85);
		
		KeyValueBaseImplService serv = new KeyValueBaseImplService(); 
		KeyValueBaseImpl port = serv.getKeyValueBaseImplPort();
		initTime = System.currentTimeMillis();
		
		while(true){
			
			int next = distr.next();
			k.setKeydata(next);
			startTime = System.currentTimeMillis();	
			try {
				port.read(k);
			} catch (IOException_Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (KeyNotFoundException_Exception e) {
				// TODO Auto-generated catch block
			} catch (ServiceNotInitializedException_Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			endTime = System.currentTimeMillis();
			total += (endTime - startTime);
			counter++;
			
			if(System.currentTimeMillis()-initTime >= 10000){
				System.out.println("Average Latency over the course of 10 seconds: "+ (total/counter) + "ms" );
				counter = 0;
				initTime = System.currentTimeMillis();
				total = 0;
			}
		}
	}
}
